package com.zi.experttalk.experttalk.View;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import com.zi.experttalk.experttalk.Model.service.HttpManager;
import com.zi.experttalk.experttalk.R;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * Created by lenovo on 2014/12/10.
 */
public class RegisterActivity extends Activity {
    private Button register_back_Button;
    private EditText register_userEmail_EditText;
    private EditText register_username_EditText;
    private EditText register_userPassWord_EditText;
    private EditText register_userPassWord2_EditText;
    private EditText register_userTell_EditText;
    private RadioButton register_sex_RadioButton;
    private RadioButton register_sex2_RadioButton;
    private Button register_register_Button;
    private int sex;
    private String userName;
    private String userEmail;
    private String passWord;
    private String passWord2;
    private String userTell;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.register );
        register_back_Button = (Button) findViewById(R.id.register_back_Button);
        register_userEmail_EditText = (EditText) findViewById(R.id.register_userEmail_EditText);
        register_username_EditText = (EditText) findViewById(R.id.register_username_EditText);
        register_userPassWord_EditText = (EditText) findViewById(R.id.register_userPassWord_EditText);
        register_userPassWord2_EditText = (EditText) findViewById(R.id.register_userPassWord2_EditText);
        register_userTell_EditText = (EditText) findViewById(R.id.register_userTell_EditText);
        register_sex_RadioButton = (RadioButton) findViewById(R.id.register_sex_RadioButton);
        register_sex2_RadioButton = (RadioButton) findViewById(R.id.register_sex2_RadioButton);
        register_register_Button = (Button) findViewById(R.id.register_register_Button);
        register_back_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(RegisterActivity.this,HomeActivity.class);
//                intent.putExtra("back", "3");
//                startActivity(intent);
                finish();
            }
        });
        userEmail = register_userEmail_EditText.getText().toString();
        userName = register_username_EditText.getText().toString();
        passWord = register_userPassWord_EditText.getText().toString();
        passWord2 = register_userPassWord2_EditText.getText().toString();
        userTell = register_userTell_EditText.getText().toString();
        if (register_sex_RadioButton.isChecked()==true) {
            sex = 0;
        }else {
            sex = 1;
        }
        final String registerParam = "{\"gender\":\""+sex
                                               +"\",\"user_name\":\""+userName
                                               +"\",\"user_phoneNum\":\""+userTell
                                               +"\",\"act\":\"register\",\"password\":\""+passWord
                                               +"\",\"email\":\""+userEmail+"\"}";
        
        register_register_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!passWord.equals(passWord2)) {
                    Toast.makeText(RegisterActivity.this,"请再次确认密码",Toast.LENGTH_SHORT).show();
                }else if (!userEmail.contains("@")) {
                    Toast.makeText(RegisterActivity.this,"请检查邮箱格式",Toast.LENGTH_SHORT).show();
                }else if (passWord.length() < 4) {
                    Toast.makeText(RegisterActivity.this,"请确保密码长度大于四位",Toast.LENGTH_SHORT).show();
                }else if (userTell.length() != 11) {
                    Toast.makeText(RegisterActivity.this,"请检查电话号码",Toast.LENGTH_SHORT).show();
                }else{
                    HttpManager httpManager = new HttpManager();
                    try {
                        httpManager.getHttpInfo(registerParam, new HttpManager.GetDataResultListener() {
                            @Override
                            public void onResult(String data) {
                                if (data != null) {
                                    try {
                                        manageData(data);

                                    } catch (JSONException e) {
                                        Toast.makeText(RegisterActivity.this,"解析有误",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        });
                    } catch (IOException e) {
                        Toast.makeText(RegisterActivity.this,"网络请求失败",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    public void manageData(String data) throws JSONException {
        JSONObject object = new JSONObject(data);
        String info = object.optString("info");
        Toast.makeText(RegisterActivity.this,info,Toast.LENGTH_SHORT).show();
        int returnResource = object.getInt("return");
        if (returnResource == 1) {
            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(RegisterActivity.this,UserActivity.class);
            intent.putExtra("back",userName);
            setResult(RESULT_OK,intent);

        }else {
            Toast.makeText(RegisterActivity.this,info,Toast.LENGTH_SHORT).show();
        }
    }

}
